<?php

namespace app\admin\model\chase;

use think\Db;
use think\Model;

class RecipeImages extends Model
{
    public static function createOrUpdate($recipe_id, $images)
    {
        $images = explode(',', $images);
        self::del($recipe_id, $images);
        if ($images) {
            $data = [];
            foreach ($images as $image) {
                if (in_array($image, self::images($recipe_id))) {
                    continue ;
                }
                $data[] = [
                    'recipe_id' => $recipe_id,
                    'image' => $image
                ];
            }
            self::insertAll($data);
        }

    }

    public static function del($recipe_id, $images)
    {
        return self::where('recipe_id', $recipe_id)->whereNotIn('image', $images)->delete();
    }

    public static function images($recipe_id)
    {
        return self::where('recipe_id', $recipe_id)->column('image');
    }
}
